//
//  LCCircleProgressView.m
//  HQBProject
//
//  Created by taolei-mac on 2017/4/18.
//  Copyright © 2017年 com.zhejiangchain.ios. All rights reserved.
//

#define DEGREES_DefaultStart(degrees)  ((M_PI * (degrees+270))/ 180) //默认270度为开始的位置

#import "LCCircleProgressView.h"

@interface LCCircleProgressView ()<CAAnimationDelegate>

@property (nonatomic, assign) CGFloat endPercent;
@property (nonatomic, strong) CAShapeLayer *animationLayer;

@end

@implementation LCCircleProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        
        self.endPercent = 0;
        [self drawEdgeCircle];
        
        [self drawInsideCircle];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if(self){
        self.endPercent = 0;
        [self drawEdgeCircle];
        
        [self drawInsideCircle];
    }
    return self;
}


/**
 *  外部的大圆环
 */
- (void)drawEdgeCircle
{
    UIBezierPath *edgePath = [UIBezierPath bezierPath];
    [edgePath addArcWithCenter:CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5)
                        radius:self.frame.size.width * 0.5 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    CAShapeLayer *edgeLayer = [CAShapeLayer layer];
    edgeLayer.path = edgePath.CGPath;
    edgeLayer.lineWidth = 2.0;
    edgeLayer.fillColor = nil;
    edgeLayer.strokeColor = LCColor(184, 184, 184).CGColor;
    edgeLayer.frame = self.bounds;
    [self.layer addSublayer:edgeLayer];
}

/**
 *  内部的小圆环
 */
- (void)drawInsideCircle
{
    UIBezierPath *insideCirclePath = [UIBezierPath bezierPath];
    [insideCirclePath addArcWithCenter:CGPointMake(self.width * 0.5, self.height * 0.5) radius:self.width * 0.5 - 15
                            startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    CAShapeLayer *edgeLayer = [CAShapeLayer layer];
    edgeLayer.path = insideCirclePath.CGPath;
    edgeLayer.lineWidth = 2.0;
    edgeLayer.fillColor = nil;
    edgeLayer.strokeColor = LCColor(184, 184, 184).CGColor;
    edgeLayer.frame = self.bounds;
    [self.layer addSublayer:edgeLayer];
}

- (void)makeProgressCircle:(NSInteger)percent withAnimationTime:(CGFloat)animationTime
{
    self.endPercent = percent / 100.0;
    if(self.endPercent > 1.0){
        self.endPercent = 1.0;
    }
    [self addProgressCircleWithPercent:percent andAnimationTime:animationTime];
}

/**
 *  添加进度圆环
 */
- (void)addProgressCircleWithPercent:(NSInteger)percent andAnimationTime:(CGFloat)animationTime
{
    UIBezierPath *progressCirclePath = [UIBezierPath bezierPath];
    [progressCirclePath addArcWithCenter:CGPointMake(self.width * 0.5, self.height * 0.5) radius:self.width * 0.5 - 15
                              startAngle:DEGREES_DefaultStart(0) endAngle:DEGREES_DefaultStart(360) clockwise:YES];
    
    if(!self.animationLayer){
        self.animationLayer = [CAShapeLayer layer];
        self.animationLayer.lineWidth = 2.0;
        self.animationLayer.fillColor = nil;
        self.animationLayer.strokeColor = [UIColor whiteColor].CGColor;
        self.animationLayer.frame = self.bounds;
        [self.layer addSublayer:self.animationLayer];
    }
    
    self.animationLayer.path = progressCirclePath.CGPath;
    [self addProgressAnimation:self.animationLayer andAnimationTime:animationTime];
}

- (void)addProgressAnimation:(CAShapeLayer *)shapeLayer andAnimationTime:(CGFloat)animationTime
{
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = animationTime;
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pathAnimation.fromValue = [NSNumber numberWithFloat:0];
    pathAnimation.toValue = [NSNumber numberWithFloat:self.endPercent];
    pathAnimation.autoreverses = NO;
    pathAnimation.delegate = self;
    [shapeLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
}

#pragma mark -- CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    UIBezierPath *animationCirclePath = [UIBezierPath bezierPath];
    [animationCirclePath addArcWithCenter:CGPointMake(self.width * 0.5, self.height * 0.5) radius:self.width * 0.5 - 15
                               startAngle:DEGREES_DefaultStart(0) endAngle:DEGREES_DefaultStart(self.endPercent * 360) clockwise:YES];
    self.animationLayer.path = animationCirclePath.CGPath;
}



@end
